﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace GSF
{
    class Terrains:GameComponent
    {
        private List<Texture2D> m_texture2DList;
        int[,] map = new int[,] 
        {
            {0,0,1,0,0,0,0,0,},
            {0,0,1,1,0,0,0,0,},
            {0,0,0,1,1,0,0,0,},
            {0,0,0,0,1,0,0,0,},
            {0,0,0,1,1,0,0,0,},
            {0,0,1,1,0,0,0,0,},
            {0,0,1,0,0,0,0,0,},
            {0,0,1,1,1,1,1,1,},
        };
        Texture2D m_Background;
        int m_blockWidth;
        int m_blockHeight;
        int m_colums;
        int m_rows;
        Vector2 m_startPoint;

        public int SideLen
        {
            get
            {
                return m_blockHeight;
            }
        }

        Queue<Vector2> m_waypoints;

        public Terrains(Game game)
            :base(game)
        {
            m_blockHeight = m_blockWidth = 59;
            m_colums = m_rows = 8;
            m_startPoint = new Vector2(2, 0) * m_blockHeight;
        }

        public void Init(Game game)
        {
            m_texture2DList = new List<Texture2D>();
            m_texture2DList.Add((Texture2D)game.Content.Load<Texture2D>(ResourceMgr.Instance.getTerrainPath(0)));
            m_texture2DList.Add((Texture2D)game.Content.Load<Texture2D>(ResourceMgr.Instance.getTerrainPath(1)));
            genMap();
        }

        void genMap()
        {
            Vector2 currentPoint = m_startPoint;
            Vector2 lastPoint = m_startPoint;
            Vector2 newPoint;
            m_waypoints = new Queue<Vector2>();
            m_waypoints.Enqueue(currentPoint);
            int j =(int) m_startPoint.X/m_blockHeight;
            int i =(int) m_startPoint.Y/m_blockHeight;
            while (true)
            {
                if (i > 0)
                    if (map[i - 1, j] == 1)
                    {
                        i--;
                        newPoint = new Vector2(j, i) * m_blockHeight;
                        if (newPoint != lastPoint)
                        {
                            lastPoint = currentPoint;
                            currentPoint = newPoint;
                            m_waypoints.Enqueue(currentPoint);
                            continue;
                        }
                        i++;
                    }
                if (i < m_colums - 1)
                    if (map[i + 1, j] == 1)
                    {
                        i++;
                        newPoint = new Vector2(j, i) * m_blockHeight;
                        if (newPoint != lastPoint)
                        {
                            lastPoint = currentPoint;
                            currentPoint = newPoint;
                            m_waypoints.Enqueue(currentPoint);
                            continue;
                        }
                        i--;
                    }
                if (j > 0)
                    if (map[i, j - 1] == 1)
                    {
                        j--;
                        newPoint = new Vector2(j, i) * m_blockHeight;
                        if (newPoint != lastPoint)
                        {
                            lastPoint = currentPoint;
                            currentPoint = newPoint;
                            m_waypoints.Enqueue(currentPoint);
                            continue;
                        }
                        j++;
                    }
                if (j < m_rows - 1)
                    if (map[i, j + 1] == 1)
                    {
                        j++;
                        newPoint = new Vector2(j, i) * m_blockHeight;
                        if (newPoint != lastPoint)
                        {
                            lastPoint = currentPoint;
                            currentPoint = newPoint;
                            m_waypoints.Enqueue(currentPoint);
                            continue;
                        }
                        j--;
                    }
                break;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            for(int i = 0;i<m_rows;i++)
                for (int j = 0; j < m_colums; j++)
                {
                    int index = map[i, j];
                    Texture2D texture = m_texture2DList[index];

                    spriteBatch.Draw(texture, new Rectangle(j * m_blockWidth, i * m_blockHeight, m_blockWidth, m_blockHeight), Color.White);
                }
        }

        public Queue<Vector2> WayPoints
        {
            get
            {
                return m_waypoints;
            }
        }
    }
}